package com.xin.auto.lib.base;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.ArrayList;

public class DbHelper extends SQLiteOpenHelper {
    private static final String TAG = "DbHelper";
    public static final String YEAR = "year";
    public static final String MONTH = "month";
    public static final String MONTH_HUMANS = "humanNums";
    public static final String H_ID = "hId";
    public static final String HUMANS_TABLE = "HumansTable";

    public DbHelper(@Nullable Context context) {
        super(context, "humans.db", null, 6);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(TAG, "db create!!!!");
        String createHumansState = "CREATE TABLE " + HUMANS_TABLE + "(" + H_ID + " Integer PRIMARY KEY AUTOINCREMENT, "
                + YEAR + " Text, "
                + MONTH + " Text, "
                + MONTH_HUMANS + " Integer)";

        String createValidateState = "CREATE TABLE ValidteTable (vId Integer PRIMARY KEY AUTOINCREMENT, code Text)";
        db.execSQL(createHumansState);
        db.execSQL(createValidateState);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + HUMANS_TABLE);
        onCreate(db);
    }

    public void updateIfExist(Humans humans) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("replace INTO HumansTable (hId, year, month, humanNums) VALUES ("
                + humans.gethId() + "," + CalendarUtils.getYear() + ", " + humans.getMonth() + ", " + humans.getHumanNums() + ");");
        db.close();
    }

    public void updateIfValidateExist(String code) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("replace INTO ValidteTable (vId, code) VALUES (1, \"" + code + "\");");
        db.close();
    }

    public int selectMonthValue(int month) {
        SQLiteDatabase db = this.getWritableDatabase();
        //Hash map, as we did in bundles
        Cursor cursor = db.rawQuery("select * from " + HUMANS_TABLE + " where year=" + CalendarUtils.getYear() + " AND month=" + month + ";", null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(MONTH_HUMANS);
                if (index != -1) {
                    int humans = cursor.getInt(index);
                    Log.i(TAG, "selectMonthValue index: " + humans);
                    return humans;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "selectMonthValue e" + e);
        }

        return 0;
    }

    public String selectValidateCode() {
        SQLiteDatabase db = this.getWritableDatabase();
        //Hash map, as we did in bundles
        Cursor cursor = db.rawQuery("select * from ValidteTable where vId=1;", null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                int index = cursor.getColumnIndex("code");
                if (index != -1) {
                    return cursor.getString(index);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "selectMonthValue e" + e);
        }

        return "";
    }

    public int selectCurrentDay() {
        SQLiteDatabase db = this.getWritableDatabase();
        //Hash map, as we did in bundles
        Cursor cursor = db.rawQuery("select * from " + HUMANS_TABLE + " where " + MONTH + "=-?", new String[]{String.valueOf(-1)});
        if (cursor != null && cursor.moveToFirst()) {
            int index = cursor.getColumnIndex(MONTH_HUMANS);
            return cursor.getInt(index);
        }
        return 0;
    }

    public void update(Humans humans) {
        SQLiteDatabase db = this.getWritableDatabase();
        //Hash map, as we did in bundles
        ContentValues cv = new ContentValues();
        cv.put(MONTH, humans.getMonth());
        cv.put(MONTH_HUMANS, humans.getHumanNums());
        db.update(HUMANS_TABLE, cv, "month = ?", new String[]{humans.getMonth()});
        db.close();
    }

    public void delete(Humans humans) {
        SQLiteDatabase db = this.getWritableDatabase();
        //Hash map, as we did in bundles
        ContentValues cv = new ContentValues();
        cv.put(YEAR, humans.getYear());
        cv.put(MONTH, humans.getMonth());
        cv.put(MONTH_HUMANS, humans.getHumanNums());
        if (humans.gethId() != -1) {
            db.delete(HUMANS_TABLE, "year = ? AND month = ? ", new String[]{humans.getYear(), humans.getMonth()});
        }
        db.close();
    }

    public ArrayList<Humans> getAllHumans() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursorCourses = db.rawQuery("SELECT * FROM " + HUMANS_TABLE, null);
        ArrayList<Humans> humansArrayList = new ArrayList<>();

        // moving our cursor to first position.
        if (cursorCourses.moveToFirst()) {
            do {
                Humans humans = new Humans(cursorCourses.getString(1),cursorCourses.getString(2),
                        Math.max(cursorCourses.getInt(3), 0));
                humansArrayList.add(humans);
            } while (cursorCourses.moveToNext());
        }

        cursorCourses.close();
        return humansArrayList;
    }
}
